﻿using System;
using Artem.Google.UI;
using BikeBouncer.BusinessLayer;
using BikeBouncer.BusinessLayer.Entities;
using BikeBouncer.Site.Enums;

namespace BikeBouncer.Site
{
	public partial class BlackMarkets_aspx : BasePage
	{
		#region Properties

		private BlackMarketCollection Markets { get; set; }

		#endregion

		#region Main methods

		protected void Page_Load(Object sender, EventArgs e)
		{
			LoadBlackMarkets();

			PanSuggestSaved.Visible = false;

			if (!Page.IsPostBack)
			{
				TxtSearch.Text = SessionManager.CurrentCity;

				InitMap();
				BindToMap(TxtSearch.Text);

				RegisterScript(ScriptSource.ColorBox);

				AjaxManager.SetDefaults_AutoCompleteExtender(AutoComplete_TxtSuggestCity);
			}
		}

		private void InitMap()
		{
			Maps.EnableReverseGeocoding = true; 
			Maps.EnableScrollWheelZoom = true;
			Maps.Latitude = 51.5;
			Maps.Longitude = 0.15;
			Maps.Zoom = 9;

			Maps.Address = SessionManager.CurrentCountry;
		}

		#endregion

		#region Load markets

		private void LoadBlackMarkets()
		{
			Object cachedMarkets = GetCache("BlackMarkets");

			if (cachedMarkets == null)
			{
				Markets = new BlackMarketCollection(DataAccess.BlackMarket_Get());

				SetCache("BlackMarkets", Markets, CacheDuration.Default);
			}
			else
			{
				Markets = (BlackMarketCollection)cachedMarkets;
			}
		}

		#endregion

		#region Search markets

		protected void ImgSearch_Click(Object sender, EventArgs e)
		{
			if (String.IsNullOrEmpty(TxtSearch.Text))
			{
				return;
			}

			BindToMap(TxtSearch.Text.Trim());
		}

		private void BindToMap(String keywords)
		{
			Maps.Markers.Clear();

			BlackMarketCollection filtered = String.IsNullOrEmpty(keywords) ? Markets : Markets.Filter(keywords);

			foreach (BlackMarket m in filtered)
			{
				GoogleMarker marker;

				if (m.Address.Latitude != 0 && m.Address.Longitude != 0)
				{
					marker = new GoogleMarker(m.Address.Latitude, m.Address.Longitude);
				}
				else
				{
					marker = new GoogleMarker(m.Address.ToString(", "));
				}

				marker.Title = m.Title;
				marker.InfoContent.CssClass = "MapInfoWindow";
				marker.Text = "<h3>" + m.Title + "</h3><p>" + m.Address.ToString("<br />") + "</p>";
				Maps.Markers.Add(marker);
			}
		}

		#endregion

		#region Suggest

		protected void ButSuggestSend_Click(Object sender, EventArgs e)
		{
			SendSuggestion();
		}

		private void SendSuggestion()
		{
			if (TxtSuggestTitle.Text.Length < 2 && TxtSuggestPostcode.Text.Length < 2 && TxtSuggestLocation.Text.Length < 2)
			{
				return;
			}

			BlackMarket m = new BlackMarket();
			m.Address = new Address();

			m.Title = TxtSuggestTitle.Text;
			m.Address.Postcode = TxtSuggestPostcode.Text;
			m.Address.Location = TxtSuggestLocation.Text;
			m.Address.City.Title = TxtSuggestCity.Text;

			try
			{
				MailManager.SuggestBlackMarket(m);

				String title = TxtSuggestTitle.Text;

				if (String.IsNullOrEmpty(title))
				{
					title = TxtSuggestLocation.Text;
				}

				if (String.IsNullOrEmpty(title))
				{
					title = TxtSuggestPostcode.Text;
				}

				TxtSuggestTitle.Text = String.Empty;
				TxtSuggestPostcode.Text = String.Empty;
				TxtSuggestLocation.Text = String.Empty;
				TxtSuggestCity.Text = String.Empty;

				LblSuggestSaved.Text = GetLocalResourceObject("LblSuggestSaved").ToString().Replace("#TITLE#", title);
				PanSuggestSaved.Visible = true;
			}
			catch (Exception ex)
			{
				ErrorManager.Log(ex, "Could not send Black Market suggestion.");
			}
		}

		#endregion
	}
}